#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
using ll=long long;
using ul=unsigned long long;
using vt=vector<int>;
int T,n,m,a[N],b[N],ct,t1,t2;
ll ans,sum;
bitset<N>vs;
struct dat{
    int x,y;
    void rd(){cin>>x>>y;}
    int operator*(const dat &z)const{
        return max(abs(x-z.x),abs(y-z.y));
    }
    bool operator<(const dat &z)const{
        return x<z.x;
    }
}d[N],v[N],v1[N];
int X[N],xt,Y[N],yt;
int main(){
    ios::sync_with_stdio(false),cin.tie(0);
    freopen("chess.in","r",stdin);
    freopen("chess.out","w",stdout);
    int i,j,k,l,r,x,y,z;
    n=1e5,vs[1]=1;
    for(x=2;x<=n;++x)
        if(!vs[x])
            for(y=x+x;y<=n;y+=x)
                vs[y]=1;
    for(x=11;x<=n;++x)
        if(x%20!=3)vs[x]=1;
    for(cin>>T;T--;){
        cin>>n>>k,sum=ans=0;
        for(i=1;i<=n;++i){
            cin>>x>>y;
            d[i]={x+y,x-y};
        }
        for(i=1;i<=n;++i)cin>>a[i],sum+=a[i];
        if(n<3){puts("0");continue;}
        xt=yt=0;
        for(x=1;x<=n;++x){
            X[x]=d[x].x;
            Y[x]=d[x].y;
        }
        sort(X+1,X+n+1),xt=unique(X+1,X+n+1)-X-1;
        sort(Y+1,Y+n+1),yt=unique(Y+1,Y+n+1)-Y-1;
        for(x=1;x<=n;++x){
            for(i=x+1,ct=t1=0;i<=n;++i)
                v[++ct]={d[x]*d[i],a[i]};
            for(i=1;i<x;++i)
                v1[++t1]={d[x]*d[i],a[i]};
            sort(v+1,v+ct+1);
            sort(v1+1,v1+t1+1);
            ll s1,s2;
            for(l=1,s1=z=0;l<=ct;l=r+1){
                for(r=l;r<ct&&v[r+1].x==v[l].x;++r);
                if(!vs[v[l].x]){
                    for(i=l,s2=0;i<=r;++i)s2+=v[i].y;
                    ans+=s2*(r-l);
                    ans+=1ll*a[x]*(r-l+1)*(r-l)/2;
                    while(z<t1&&v1[z+1].x<v[l].x)s1+=v1[++z].y;
                    ans+=(1ll*a[x]*(r-l+1)+s2)*(z+l-1);
                    ans+=s1*(r-l+1);
                }for(i=l;i<=r;++i)s1+=v[i].y;
            }
            for(l=1,s1=z=0;l<=t1;l=r+1){
                for(r=l;r<t1&&v1[r+1].x==v1[l].x;++r);
                if(!vs[v1[l].x]){
                    for(i=l,s2=0;i<=r;++i)s2+=v1[i].y;
                    ans+=s2*(r-l);
                    ans+=1ll*a[x]*(r-l+1)*(r-l)/2;
                    while(z<ct&&v[z+1].x<=v1[l].x)s1+=v[++z].y;
                    ans+=(1ll*a[x]*(r-l+1)+s2)*(z+l-1);
                    ans+=s1*(r-l+1);
                }for(i=l;i<=r;++i)s1+=v1[i].y;
            }
        }
        for(l=1;l<=n;++l)
            for(r=l+1;r<=n;++r)
                if(!vs[d[l]*d[r]]){
                    k=d[l]*d[r];
                    for(i=1;i<l;++i){
                        if(d[l]*d[i]<k&&d[r]*d[i]<k)
                            ans-=a[l]+a[i]+a[r]<<1;
                    }
                    for(i=l+1;i<r;++i){
                        if(d[l]*d[i]<k&&d[r]*d[i]<=k)
                            ans-=a[l]+a[i]+a[r]<<1;
                    }
                    for(i=r+1;i<=n;++i){
                        if(d[l]*d[i]<=k&&d[r]*d[i]<=k)
                            ans-=a[l]+a[i]+a[r]<<1;
                    }
                }
        printf("%lld\n",ans);
    }
    return 0;
}